
package com.hc.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hc.domain.Article;
import com.hc.domain.Nodes;
import com.hc.domain.Role;
import com.hc.service.IArticleService;
import com.hc.service.INodesService;
import com.hc.service.IRoleService;
import com.hc.service.IUserAndRoleService;
import com.hc.util.TaskThreadProvider;
import com.hc.vo.NodesVo;
import com.hc.vo.RoleVo;

@Controller
@RequestMapping("View")
public class ViewController {
	@Resource
	private IRoleService roleService;
	@Resource
	private INodesService nodesService;
	@Resource
	private IUserAndRoleService userAndRoleService;
	@Resource
	private TaskThreadProvider taskProvider;
	@Resource
	private IArticleService articleService;

	/**
	 * 
	 * @Description： 跳转根路径登录界面
	 * 
	 * @param req
	 * @return
	 * @author:
	 * @since: 2017年5月11日 下午2:04:32
	 */
	@RequestMapping(value = "/background.login", method = RequestMethod.GET)
	public String index(HttpServletRequest req) {
		return "login";
	}

	@RequestMapping(value = "/pages/{name}", method = RequestMethod.GET)
	public ModelAndView viewr(HttpServletRequest req, HttpServletResponse resp, @PathVariable String name,
			ModelMap modelMap) {
		name = "/module/" + name;
		return new ModelAndView(name, modelMap);
	}

	@RequestMapping(value = "/pages/{name}/{type}/{id}", method = RequestMethod.GET)
	public ModelAndView viewrCid(HttpServletRequest req, HttpServletResponse resp, @PathVariable String name,
			@PathVariable String type, @PathVariable int id, ModelMap modelMap) {
		name = "/module/" + name;
		if ("edit".equals(type)) {
			Article a = articleService.getById(id);
			modelMap.addAttribute("art", a);
		}
		return new ModelAndView(name, modelMap);
	}

	@RequestMapping(value = "/web/{path}")
	@ResponseBody
	public ModelAndView web(HttpServletRequest req, HttpServletResponse resp, @PathVariable String path,
			ModelMap modelMap) {
		/***********************************
		 * 获取所有角色数据并跳转对应页面
		 ***********************************/
		if (path.equals("role")) {
			Future<List<RoleVo>> roleFuture = taskProvider.getThreadProvider().submit(new Callable<List<RoleVo>>() {
				@Override
				public List<RoleVo> call() throws Exception {
					List<RoleVo> RoleVo = new ArrayList<RoleVo>();
					List<Role> list = roleService.findAll();
					for (Role r : list) {
						RoleVo rv = new RoleVo();
						rv.setId(r.getId());
						rv.setRole(r.getRole());
						rv.setStatus(r.getStatus());
						int count = userAndRoleService.getRoleUserCount(r.getId());
						rv.setCount(count);
						RoleVo.add(rv);
					}
					return RoleVo;
				}
			});

			try {
				modelMap.addAttribute("role", roleFuture.get());
			} catch (Exception e) {
				modelMap.addAttribute("role", null);
			}
			Future<List<NodesVo>> nodeFuture = taskProvider.getThreadProvider().submit(new Callable<List<NodesVo>>() {
				@Override
				public List<NodesVo> call() throws Exception {
					List<NodesVo> nodesVo = new ArrayList<NodesVo>();
					List<Nodes> nodes = nodesService.getNodesByParentId(0);
					if (nodes != null && nodes.size() != 0) {
						for (Nodes n : nodes) {
							NodesVo nv = new NodesVo();
							nv.setIcon(n.getIcon());
							nv.setId(n.getId());
							nv.setNode(n.getNode());
							nv.setParentId(n.getParentId());
							nv.setPath(n.getPath());
							List<Nodes> childNodes = new ArrayList<Nodes>();
							if (n.getStatus() == 0) {
								childNodes = nodesService.getNodesByParentId(n.getId());
							}
							nv.setNodes(childNodes);
							nodesVo.add(nv);
						}
					}
					return nodesVo;
				}
			});

			try {
				modelMap.addAttribute("nodes", nodeFuture.get());
			} catch (Exception e) {
				modelMap.addAttribute("nodes", null);
			}
		}
		/************************************
		 * 获取对应节点数据，并跳转对应页面
		 ********************************/
		if (path.equals("node")) {
			List<NodesVo> nodesVo = new ArrayList<NodesVo>();
			List<Nodes> nodes = nodesService.getNodesByParentId(0);
			if (nodes != null && nodes.size() != 0) {
				for (Nodes n : nodes) {
					NodesVo nv = new NodesVo();
					nv.setIcon(n.getIcon());
					nv.setId(n.getId());
					nv.setNode(n.getNode());
					nv.setParentId(n.getParentId());
					nv.setPath(n.getPath());
					List<Nodes> childNodes = new ArrayList<Nodes>();
					if (n.getStatus() == 0) {
						childNodes = nodesService.getNodesByParentId(n.getId());
					}
					nv.setNodes(childNodes);
					nodesVo.add(nv);
				}
			}
			modelMap.addAttribute("nodes", nodesVo);
		}
		return new ModelAndView(path, modelMap);
	}
}
